﻿Imports System.Data.OleDb
Public Class ProfileEdit
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim sql As String = "SELECT * FROM Profiles WHERE userName=@f8"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f8", Membership.GetUser(User.Identity.Name).ProviderUserKey)


            Dim odr = cmd.ExecuteReader

            odr.Read()


            If Not IsDBNull(odr("FirstName")) Then tb_fname.Text = odr("FirstName")
            If Not IsDBNull(odr("LastName")) Then tb_lname.Text = odr("LastName")
            If Not IsDBNull(odr("Gender")) Then ddl_gender.SelectedValue = odr("Gender")
            If Not IsDBNull(odr("DoB")) Then tb_dob.Text = odr("DoB")
            If Not IsDBNull(odr("PreferredOS")) Then ddl_os.SelectedValue = odr("PreferredOS")
            If Not IsDBNull(odr("PreferredCPU")) Then ddl_cpu.SelectedValue = odr("PreferredCPU")
            If Not IsDBNull(odr("PreferredScreenResolution")) Then ddl_resolution.SelectedValue = ("PreferredScreenResolution")
        End If




    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click

        Dim newFileName As String

        If fup_userpic.HasFile Then


            newFileName = Guid.NewGuid().ToString() & getSuffix(fup_userpic.FileName)
            If fup_userpic.HasFile Then
                fup_userpic.SaveAs(Server.MapPath("Pictures") & "/" & newFileName)
            End If
        End If

        Dim sql As String

        If fup_userpic.HasFile Then

            sql = "UPDATE Profiles SET FirstName=@f1, LastName=@f2, UserPic=@f3, Gender=@f4, PreferredOS=@f5, PreferredCPU=@f6, PreferredScreenResolution=@f7, DoB=@f8 WHERE userName=@f9"
        Else
            sql = "UPDATE Profiles SET FirstName=@f1, LastName=@f2, Gender=@f4, PreferredOS=@f5, PreferredCPU=@f6, PreferredScreenResolution=@f7, DoB=@f8 WHERE userName=@f9"
        End If

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("connstr").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", tb_fname.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lname.Text)
        If fup_userpic.HasFile Then cmd.Parameters.AddWithValue("@f3", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", ddl_gender.SelectedValue)
        cmd.Parameters.AddWithValue("@f5", ddl_os.SelectedValue)
        cmd.Parameters.AddWithValue("@f6", ddl_cpu.SelectedValue)
        cmd.Parameters.AddWithValue("@f7", ddl_resolution.SelectedValue)
        cmd.Parameters.AddWithValue("@f8", tb_dob.Text)
        cmd.Parameters.AddWithValue("@f9", Membership.GetUser(User.Identity.Name).ProviderUserKey)

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class